Page({
  data: {
    orders: []
  },

  onLoad(options) {
    const savedOrders = wx.getStorageSync('orders') || [];
    this.setData({
      orders: savedOrders
    });
  },

  updateState(e) {
    const id = e.currentTarget.dataset.id;
    let stateID = e.currentTarget.dataset.stateId;
    var startTime1 = e.currentTarget.dataset.startTime;
    var endTime1 = e.currentTarget.dataset.endTime;
    const now = new Date();

// 构造开始时间为 2025-01-06 00:00:00 的 Date 对象.getTime();
const startTime = (new Date(startTime1 + "T00:00:00"))

// 构造结束时间为 2025-01-06 23:59:59 的 Date 对象.getTime();
const endTime = new Date(endTime1 + "T23:59:59");
const endTimestamp = endTime

    if (stateID === 0 || stateID === 1 || stateID === 3) {
      stateID = 2; // 更新为“已使用”
    } else if (stateID === 2) {
      stateID = 0; // 默认为“待领取”
      console.log(endTimestamp);
      console.log(startTime);
      console.log(now);
      if (endTimestamp < now) {
        stateID = 3; // 已过期
      } else if (startTime > now) {
        stateID = 0; // 待领取
      } else {
        stateID = 1; // 生效中
      }

    }

    const orders = this.data.orders.map(order => {
      if (order.id === id) {
        order.stateID = stateID;
      }
      return order;
    });

    this.setData({
      orders: orders
    });
    wx.setStorageSync('orders', orders);
  },

  deleteOrder(e) {
    const id = e.currentTarget.dataset.id;
    const orders = this.data.orders.filter(order => order.id !== id);

    this.setData({
      orders: orders
    });
    wx.setStorageSync('orders', orders);
  }
});